<div class="users form">
<?php e($form->create('User')); ?>
    <fieldset>
         <legend><?php __('Add User');?></legend>
        <?php e($form->input('username', array(
                                'label'=>__('User Name', true),
                                'class'=>'input text',
                                'autocomplete'=>'off'))); ?>
        <?php e($form->input('password', array(
                                'label'=>__('User Password', true),
                                'value'=>'000000',
                                'type'=>'password',
                                'class'=>'input text'))); ?>
        <?php e($form->input('usedefault', array(
                                'label'=>__('Use default password(000000)', true),
                                'type'=>'checkbox',
                                'checked'=>'checked',
                                'class'=>'checkbox'))); ?>
        <?php e($form->hidden('type', array('value'=>'1', 'class'=>'hidden'))); ?>
        <?php e($form->hidden('active', array('value'=>'1', 'class'=>'hidden'))); ?><br class="clear"/><br class="clear"/>
        <?php e($form->submit(__('Add User', true), array('class'=>'submit attention'))); ?>
    </fieldset>
<?php e($form->end()); ?>
</div>
<script type="text/javascript">
$(window).ready(function(){
    $('label[for=UserUsedefault]').addClass('checkbox');
    $('input#UserPassword').attr({'disabled': 'disabled', 'value': '000000'});
    // Change password field disable status of user check the UseDefault checkbox
    $('input#UserUsedefault').change(function(){
        if($(this).attr('checked')){
            $('input#UserPassword').attr({'disabled': 'disabled', 'value': '000000'}).blur();
        }else{
            $('input#UserPassword').removeAttr('disabled').focus().clearFields();
        }
    });
    // Validate UserAddForm
    $.sme.data.user = false;
    $('#UserAddForm').submit(function(){
        if(!$('#UserAddForm').valid()){
            $.log('Add user with invalid values, drop the request');
            return false;
        }
        if(!$.sme.status.user){
            alert('该用户名已经存在, 不能添加');
            $.log('User name taken, use another');
            return false;
        }
        // We have to enable the password field, to send to server
        $('input#UserPassword').removeAttr('disabled');
    }).validate({
        success: function(label){
            $.sme.cbk.ajaxCheck.apply({
                label: label,
                input: label.prev('input'),
                id: 'UserUsername',
                model: 'User',
                control: 'user',
                fielden: 'username',
                fieldzh: '用户名'
            });
        },
        rules: {
            'data[User][username]': 'required',
            'data[User][password]': 'required'
        },
        messages: {
            'data[User][username]': '用户名应该只包含数字和字母, 并且方便记忆',
            'data[User][password]': '如果没有选中使用默认密码, 请自行设置密码'
        }
    });
});
</script>